package cn.wolfcode.wolf2w.service.imp;

import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.DBHelper;
import netscape.security.UserTarget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
//@Transactional  暂时先别管事务
public class StrategyCommentServiceImpl implements IStrategyCommentService {


    @Autowired
    private StrategyCommentRepository repository;
    @Autowired
    private MongoTemplate template;

    public StrategyComment get(String id) {
        Optional<StrategyComment> optional = repository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }

        return null;
    }

    //page对象里面包括: 总页数 每页显示条数......
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getStrategyId())) {
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        return DBHelper.query(template, StrategyComment.class, query, qo.getPageable());
    }


    @Override
    public void addComment(StrategyComment comment) {

        //时间
        comment.setCreateTime(new Date());
        comment.setId(null);

        repository.save(comment);

    }

    @Override
    public void commentThumb(String cid, String uid) {
        StrategyComment strategyComment = this.get(cid);
        List<String> strategyCommentList = strategyComment.getThumbuplist();
        if (strategyCommentList.contains(uid)) {
            //存在
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() - 1);
            strategyCommentList.remove(uid);
        } else {
            //不存在
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() + 1);
            strategyCommentList.add(uid);
        }
        strategyComment.setThumbuplist(strategyCommentList);
        this.update(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        repository.save(strategyComment);
    }

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyComment.setCreateTime(new Date());
        repository.save(strategyComment);
    }


}
